import entity.*;
import entity.TableEntity;
import utils.FileUtil;
import utils.MysqlUtils;
import utils.VmUtils;

import java.util.Map;

public class Main {
    // 表名集合
//    private static String[] tableNamesStatic = {"manufacture_bom_group","manufacture_bom_info","manufacture_bom_child","manufacture_replace_group","manufacture_replace_info","manufacture_replace_main","manufacture_replace_child"};
    private static String[] tableNamesStatic = {"ums_admin"};
    // vm文件名集合
//    private static String[] vmFileNamesStatic = {"Controller.vm","Service.vm","ServiceImpl.vm","Entity.vm","Services.vm","Views.vm","Sql.vm"};
//    private static String[] vmFileNamesStatic = {"duroadminasp/Controller.vm","Service.vm","ServiceImpl.vm","Entity.vm"};
    private static String[] vmFileNamesStatic = {"Controller.vm"};

    public static void main(String[] args) {

        String projectName = "hmErpCloudAsp"; // duroAdminAsp hmErpCloudAsp
        ProjectEntity projectEntity = ProjectEntity.getProject(projectName);
        DatebaseEntity datebaseEntity = DatebaseEntity.getDatebase(projectName);
        Map<String, String> typeMap = TypeEntity.getType(projectEntity.getLanguage());
        Map<String, String[]> vmMap = VmEntity.getVm(projectName);
//        if (vmMap != null){
//            for (String str : vmMap.keySet()){
//                vmMap.get(str)[3] = null;
//            }
//        }
        String[] tableNames = tableNamesStatic;
        String[] vmFileNames = vmFileNamesStatic;
        String ctimeStr = String.valueOf(System.currentTimeMillis() / 1000);
        // 循环处理多张表集合
        for (String tableName : tableNames) {
            // 查询表信息
            TableEntity tableEntity = MysqlUtils.getTable(datebaseEntity, tableName, typeMap);
            // 模版替换符
            Map<String, Object> replaceMap = VmUtils.getReplaceMap(projectEntity, tableEntity);
            // 循环处理多个vm文件
            for (String vmFileName : vmFileNames){
                if (vmFileName.equals("Sql.vm")){
                    continue;
                }
                // vm文件存放路径
                String vmfilePath = FileUtil.vmFilePath(vmFileName, projectEntity);
                // 渲染模版后生成字符串内容
                String stringWriter = VmUtils.render(replaceMap, vmfilePath);
                // 目标文件名
//                String targetFile = FileUtil.targetFile(projectEntity, tableEntity, vmFileName, vmMap, ctimeStr);
//                System.out.println(targetFile);
                // 生成目标文件
                FileUtil.writer(stringWriter, "/Users/wencong/Documents/workspace/intellij-workspace/code-generator/file/aa.cs");
            }
        }
//        FileUtil.writerSql(tableNamesStatic, vmFileNamesStatic,datebaseEntity, projectEntity, ctimeStr,typeMap);
    }

}